#include <windows.h>
//////////////////////////////////////////////////////////////////////
// include files
#include<stdlib.h>			// exit
#include<stdio.h>			// fopen, fprintf, fclose, fscanf
#include<stddef.h>
#include<string.h>
#include"ParIn.h"
//
///////////////////////////////////////////////////////////////////
//
extern ParIn program;
extern ParIn fixation;
extern ParIn cue;
extern ParIn gapA;
extern ParIn real;
extern ParIn illusory;
extern ParIn gapB;
extern ParIn mask;
extern ParIn fillMask;
//                
// variables
//
extern int targetSteps;
extern int targetMax;
extern int dataCorrect[10];
extern int dataFalse[10];
extern int dataCorrectParallel[10];
extern int dataFalseParallel[10];
extern int dataCorrectOrthogonal[10];
extern int dataFalseOrthogonal[10];
extern int dataFalseFillMask[10];
extern int dataCorrectFillMask[10];
extern float grey;
extern float contrast;
extern bool Online;
extern bool maskline;
extern bool MoveTheProbe;
int filenumber=1;
extern float A;
extern float B;
extern float C;
extern float D;
extern float E;


extern float changedThickness;
extern int changedLength;
extern int changedPosition;
extern int changedISI;
extern float changedLuminance;
extern bool reversePhi;
extern int waittime;
extern int changedDistanceLinePairs;
extern int changedNumberLinePairs;
//
FILE *OutFile;
char *outfile="1data.txt";
char *sumfile="sum.txt";
FILE *TimeFile;
char *timefile="ReactionTime.txt";
FILE *ControlFile;
char *controlfile="Control.txt";
extern char *filename;
char OUTB[20];
char OUTA[20];
//char OUT[20];
FILE *OutFileB;
char *outfileB="1list.txt";
extern char *filenameB;
//
//
//
//_________________________________________________________________________________________________________
void WriteResults(void){
   int x=1;
   while((OutFile=fopen(outfile, "r" )) != NULL){// as long as you can open xdata.txt:
    x++;			// x=x+1,
    sprintf(OUTA,"%d",x);	// convert x to ascii-code,
    strcat(OUTA,"data.txt");	// put "x" and "data.txt" together,
    outfile=OUTA;		// name the datafile this new way
    fclose(OutFile);	// and close the old one.
  }
  if((OutFile=fopen(outfile, "a" )) == NULL) exit(1); // if you cannot open this new file, there is something really wrong - better exit.
  filename=outfile;
  fprintf(OutFile,"{\n{Thickness,Length,DistanceBetweenLines,ISI,luminance,ReversePhi, DistanceBetweenLinePairs, LinePairNumber, PauseBetweenStims},\n");
  fclose(OutFile);
  
    sprintf(OUTB,"%d",x);
    strcat(OUTB,"list.txt");	// put "x" and "data.txt" together,
    outfileB=OUTB;
    if((OutFileB=fopen(outfileB, "a" )) == NULL) exit(1);
    fclose(OutFileB);
    filenameB=outfileB;
  } 
  void WriteSettings(float grey, int length, int pos, int side, int RealLine, float realGrey){
  OutFile=fopen(filename, "a" );
  fprintf(OutFile,"{%f, %u, %d, %d, %f, %u, %u, %u, %u},\n", changedThickness, changedLength,changedPosition,changedISI,changedLuminance,reversePhi,changedDistanceLinePairs,changedNumberLinePairs,waittime);
  fclose(OutFile);
  }
 void WriteResponse(int response, int RealLine, float realGrey, int hyperacute, int LeftRight, int RealPosition){
  OutFile=fopen(filename, "a" );
  fprintf(OutFile,"{%u, %u, %f, %u, %u, %u}},\n\n{", response, RealLine, realGrey, hyperacute, LeftRight, RealPosition);
  fclose(OutFile);
  OutFileB=fopen(filenameB, "a" );
  fprintf(OutFileB,"%u , %u, %f, %u, %u, %u\n", response, RealLine, realGrey, hyperacute, LeftRight, RealPosition);
  fclose(OutFileB);
}
   
void WriteParameters(void){
  OutFile=fopen(filename, "a" );
    fprintf(OutFile,"\n{Parameter.txt\n\nMisc______________NofTrials: %u             ClearColorRGB: %f %f %f \n Fixation_msec: %u  Height: %u     Width: %u           PositionX: %d    PositionY: %d      RGB: %f %f %f\n SingleFrame_msec: %u   MaxTime: %u   LineRand: %u    LineLength: %u  LineThickness: %f     PositionX: %d    PosYToFixation: %d      Angle: %u      MinLineLength: %u  LineDist: %u  MaxSectionHeight: %u\n GreyLevel_RandSteps: %u  GreyRange: %f %f\n RealLineStart_msec: %u RealLineEnd_msec: %u  LineLength: %u  Ori_1vertical_0horiz: %u   LineThickness: %f    PosX: %d PosY: %d GreyLevel: %f}}",program.NumberOf, program.r, program.g, program.b,fixation.Time, fixation.Height, fixation.Width, fixation.PositionX,fixation.PositionY, fixation.r, fixation.g, fixation.b,illusory.Time, mask.Time, illusory.Height, illusory.Width, illusory.Thickness, illusory.PositionX, illusory.PositionY, illusory.MaxTilt, illusory.StepsTilt, mask.StepsTilt, mask.MaxTilt,gapA.Time, illusory.r, illusory.g,real.Time, gapB.Time, real.Height,  real.Width,real.Thickness, real.PositionX, real.PositionY, real.g);
    fclose(OutFile);
}

//
/*void WriteOutReactionTimes(int loop,int Cue, int Target, int Mask, int Response, float elapsedTime, int inducerTarget, int inducerMask){
	TimeFile=fopen(timefile, "a" );
	if(cue.Time!=0 && mask.Time!=0)fprintf(TimeFile,"{TrialNr:  %u  ,Response: %u,  Cue: %d   ,Target: %d   ,Mask: %d  ,msec: %f, inducer: %u, inducerMask: %u},\n",loop, Response, Cue, Target, Mask, elapsedTime, inducerTarget, inducerMask);
	if(cue.Time==0 && mask.Time!=0)fprintf(TimeFile,"{TrialNr:  %u  ,Response: %u,  Target: %d   ,Mask: %d  ,msec: %f, inducer: %u, inducerMask: %u},\n",loop,Response, Target, Mask, elapsedTime,inducerTarget, inducerMask);
	if(cue.Time!=0 && mask.Time==0)fprintf(TimeFile,"{TrialNr:  %u  ,Response: %u,  Cue: %d   ,Target: %d   ,msec: %f, inducerRand: %u},\n",loop, Response, Cue, Target, elapsedTime,inducerTarget);
	if(cue.Time==0 && mask.Time==0)fprintf(TimeFile,"{TrialNr:  %u  ,Response: %u,  Target: %d   ,msec: %f, inducerRand: %u},\n",loop,Response, Target, elapsedTime, inducerTarget);
	fclose(TimeFile);
} */

void WriteOutTimeControl(float elapsedTime, int state){
	ControlFile=fopen(controlfile, "a" );
       // fprintf(ControlFile,"username: %s \n",username);
	fprintf(ControlFile,"State: %u   elapsed Time: %f \n",state, elapsedTime);
	fclose(ControlFile);
}

void WriteOutColorControl(int redBits,int greenBits, int blueBits){
     ControlFile=fopen(controlfile, "a" );
	fprintf(ControlFile,"Bits per gun -- red %u   green %u   blue %u \n",redBits, greenBits, blueBits);
	fclose(ControlFile);
}